package com.cloudspace.jindun.activity.interphone;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.error.VolleyError;
import com.cloudspace.jindun.R;
import com.cloudspace.jindun.activity.CustomTitleBaseActivity;
import com.cloudspace.jindun.model.CommonResult;
import com.cloudspace.jindun.network.api.API;
import com.cloudspace.jindun.network.base.ApiCallback;
import com.cloudspace.jindun.user.UserSessionManager;
import com.cloudspace.jindun.utils.ToastMaker;

import org.apache.http.HttpStatus;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zengxianhua on 17/2/9.
 */

public class InterPhoneCreateActivity extends CustomTitleBaseActivity implements View.OnClickListener {
    public static final String TAG = InterPhoneCreateActivity.class.getSimpleName();

    private Button mCreateChannelBtn;
    private EditText mChannelIdEt;
    private EditText mChannelRemarkEt;
    private TextView mChannelSecretTv;
    private TextView mChannelNoSecretTv;
    private TextView mChannelAvailableTv;
    private TextView channelAvailableStaticTv;
    private EditText mChannelPasswordEt;

    private boolean mChannelIsSecret = true;
    private View.OnFocusChangeListener idFocusListener = new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (!hasFocus){
                String channelId = mChannelIdEt.getEditableText().toString();

                if (!TextUtils.isEmpty(channelId)){
                    //判断频道是否可用
                    API.getInstance().getChannelApi().channelIdAvailable(TAG, channelId, new ApiCallback<CommonResult>() {
                        @Override
                        public void handleResult(CommonResult result, VolleyError error) {
                            if (result != null){
                                channelAvailable(result.status == HttpStatus.SC_OK);
                            }
                        }
                    });
                }
            }
        }
    };

    private void channelAvailable(boolean available) {
        mChannelAvailableTv.setVisibility(View.VISIBLE);
        mChannelAvailableTv.setText(available ? getResources().getString(R.string.channel_available)
        : getResources().getString(R.string.channel_not_available));
        mChannelAvailableTv.setTextColor(available ? getResources().getColor(R.color.color_1e9ef7) :
                getResources().getColor(R.color.base_color_red));
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.duijiang_channel_create, true);
        getTitleBar().setSimpleMode(getResources().getString(R.string.channel_create));

        mCreateChannelBtn = (Button) findViewById(R.id.duijiang_btn_channel_create);
        mCreateChannelBtn.setOnClickListener(this);

        mChannelIdEt = (EditText) findViewById(R.id.duijiang_et_channel_name);
        mChannelRemarkEt = (EditText) findViewById(R.id.channel_remark);
        mChannelPasswordEt = (EditText) findViewById(R.id.channel_password);

        mChannelSecretTv = (TextView) findViewById(R.id.channel_secret);
        channelAvailableStaticTv = (TextView) findViewById(R.id.channel_static_4);
        mChannelNoSecretTv = (TextView) findViewById(R.id.channel_no_secret);
        mChannelSecretTv.setOnClickListener(this);
        mChannelNoSecretTv.setOnClickListener(this);

        mChannelAvailableTv = (TextView) findViewById(R.id.channel_available);
        mChannelAvailableTv.setVisibility(View.GONE);

        mChannelIdEt.setText("01");

        mChannelIdEt.setOnFocusChangeListener(idFocusListener);

//        mChannelRemarkEt.setText("频道01");
//        mChannelPasswordEt.setText("1234");
    }

    public static void showActivity(Context context) {
        Intent intent = new Intent(context, InterPhoneCreateActivity.class);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }

        context.startActivity(intent);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.duijiang_btn_channel_create:
                createChannel();
                break;

            case R.id.channel_secret:
                switchSecretSwitch(true);
                break;

            case R.id.channel_no_secret:
                switchSecretSwitch(false);
                break;

            default:
                break;
        }
    }

    private void switchSecretSwitch(boolean secret) {
        mChannelIsSecret = secret;

        mChannelNoSecretTv.setBackgroundResource(mChannelIsSecret ?
                R.drawable.channel_juxing : R.drawable.channel_select);
        mChannelSecretTv.setBackgroundResource(mChannelIsSecret ?
                R.drawable.channel_select : R.drawable.channel_juxing);

        mChannelPasswordEt.setText("");
        channelAvailableStaticTv.setTextColor(mChannelIsSecret ? getResources().getColor(R.color.color_424242) :
                getResources().getColor(R.color.color_c3c3c3));

        mChannelPasswordEt.setVisibility(mChannelIsSecret ? View.VISIBLE : View.GONE);
    }

    private void createChannel() {
        final String channelId = mChannelIdEt.getEditableText().toString();
        if (TextUtils.isEmpty(channelId)) {
            ToastMaker.showToastShort(getResources().getString(R.string.channel_no_name));
            return;
        }

        String channelRemark = mChannelRemarkEt.getEditableText().toString();
        if (TextUtils.isEmpty(channelRemark)) {
            ToastMaker.showToastShort(getResources().getString(R.string.channel_no_info));
            return;
        }

        final String channelPassword = mChannelPasswordEt.getEditableText().toString();
        if (TextUtils.isEmpty(channelPassword) && mChannelIsSecret) {
            ToastMaker.showToastShort(getResources().getString(R.string.channel_no_password));
            return;
        }

        List<String> userIDs = new ArrayList<>();
        userIDs.add(UserSessionManager.getInstance().getUserId());
        API.getInstance().getChannelApi().createChannel(TAG,
                channelRemark,
                channelPassword,
                channelId,
                new ApiCallback<CommonResult>() {
                    @Override
                    public void handleResult(CommonResult result, VolleyError error) {
                        if (error == null) {
                            ToastMaker.showToastShort(getResources().getString(R.string.channel_create_success));
                            finish();
                        } else {
                            ToastMaker.showToastShort(getResources().getString(R.string.channel_create_success)+
                            error.getMessage());
                        }
                    }
                }
        );
    }
}
